package models

import (
	"fmt"
	"git.inedcn.com/ined/cmn"
	"rongyy/src/app"
)

type StatisModel struct {
	Hospital  string       `json:"hospital" xorm:"-"`
	UpdatedAt cmn.JsonTime `xorm:"updated" json:"updated_at"`
}

type StatisRegReq struct {
	Begin    string `json:"begin"`
	End      string `json:"end"`
	DeptName string `json:"dept_name"`
}

type ReqStats struct {
	DeptName   string `json:"dept_name"`
	Start      string `json:"start"`
	End        string `json:"end"`
	Type       string `json:"type"`
	Month      string `json:"month"`
	WhichMonth string `json:"which_month"`
}

type StatisRegAllModel struct {
	//Name  string       `json:"name" default:"本月"`
	Date  string `json:"date"`
	Count int    `json:"count"`
}

func (pm *StatisModel) TableName() string {
	return "sys_employee"
}

func (rm *StatisModel) Reg(begin string, end string) (int64, error) {
	beginTime := begin + " 00:00:00"
	endTime := end + " 23:59:59"
	total, err := app.DB().Table("registered_orders").Where("created_at > ? and created_at < ? and pay_status = 2", beginTime, endTime).Count(rm)
	if err != nil {
		return 0, err
	}
	return total, nil
}

func (rm *StatisModel) RegAll(begin string, end string, deptName string) ([]StatisRegAllModel, error) {
	regs := make([]StatisRegAllModel, 0)
	beginTime := begin + " 00:00:00"
	endTime := end + " 23:59:59"
	s := app.DB().Table("registered_orders")

	sql := `SELECT DATE_FORMAT(created_at, '%Y-%m-%d') AS date, count( DISTINCT id ) AS count FROM registered_orders WHERE (created_at > ? AND created_at < ? AND pay_status = 2) GROUP BY date`
	//if deptName != "" {
	//	s = s.Where("reg_info->>'$.department' = ?", deptName)
	//}
	err := s.SQL(sql, beginTime, endTime).Find(&regs)
	fmt.Println(regs)
	if err != nil {
		return nil, err
	}
	return regs, nil
}

func (rm *StatisModel) PatAll(begin string, end string, deptName string) ([]StatisRegAllModel, error) {
	regs := make([]StatisRegAllModel, 0)
	beginTime := begin + " 00:00:00"
	endTime := end + " 23:59:59"
	s := app.DB().Table("ho_patientcard")

	sql := `SELECT DATE_FORMAT(created_at, '%Y-%m-%d') AS date, count( DISTINCT id ) AS count FROM ho_patientcard WHERE (created_at > ? AND created_at < ?) GROUP BY date`
	//if deptName != "" {
	//	s = s.Where("reg_info->>'$.department' = ?", deptName)
	//}
	err := s.SQL(sql, beginTime, endTime).Find(&regs)
	fmt.Println(regs)
	if err != nil {
		return nil, err
	}
	return regs, nil
}

func (rm *StatisModel) GetTotalReg(begin string, end string) (int64, error) {
	cms := make([]RegisterModel, 0)
	beginTime := begin + " 00:00:00"
	endTime := end + " 23:59:59"
	s := app.DB().Table("reg_orders")
	fmt.Println(2)
	total, err := s.Where("created_at >? and created_at < ? and pay_status = 'paid'", beginTime, endTime).FindAndCount(&cms)
	fmt.Println(3)
	if err != nil {
		return 0, err
	}
	return total, nil
}

func (rm *StatisModel) GetTotalOp(begin string, end string) (int64, error) {
	cms := make([]PrescriptionModel, 0)
	beginTime := begin + " 00:00:00"
	endTime := end + " 23:59:59"
	s := app.DB().Table("pres_orders")

	total, err := s.Where("created_at >? and created_at < ? and pay_status = 'paid'", beginTime, endTime).FindAndCount(&cms)
	if err != nil {
		return 0, err
	}
	return total, nil
}

func (rm *StatisModel) GetTotalPat(begin string, end string) (int64, error) {
	cms := make([]CardModel, 0)
	beginTime := begin + " 00:00:00"
	endTime := end + " 23:59:59"
	s := app.DB().Table("ho_patientcard")

	total, err := s.Where("created_at >? and created_at < ?", beginTime, endTime).FindAndCount(&cms)
	if err != nil {
		return 0, err
	}
	return total, nil
}
